package com.jvyou.app.common.utils.bean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 橘柚
 * @version 1.0-SNAPSHOT
 * @Date 2023/10/7 22:33
 * @Description Bean复制
 */
@Slf4j
public class BeanCopyUtils {
    private BeanCopyUtils() {
    }

    public static <T> T copyBean(Object resource, Class<T> clazz) {
        // 创建目标类型的对象
        T result = null;
        try {
            // 实例化对象
            result = clazz.newInstance();
            // bean 属性拷贝
            BeanUtils.copyProperties(resource, result);
        } catch (Exception e) {
            log.error("BeanCopyUtils copyBean error", e);
        }
        return result;
    }

    public static <O, T> List<T> copyBeanList(List<O> resourceList, Class<T> clazz) {

        return resourceList.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());

    }

}
